In this video you will learn how to create exponentially weighted moving averages.
So in a minute you will see how it works and what the differences are.
To a simple moving averages.
But first of all let's import pandas and also met plot lip.
And again we are working with the S&P 500 data set.
So we are importing the data from the S&P 500 since we fire.
Let's have a look here.
And in this video we are only working with the last 10 years.
So from 2 9 2 to 18 and we are actually here overriding our variable S&P 500.
So let's have a look again.
So here our data frame starts at the very last day of the year 2000 and 8 and we've already learned
how to create simple moving averages with the rolling method.
So by defining the rolling window and this example 10 and changing the mean method panels calculates
the rolling mean values which are actually equally weighted.
So in each timestamp the immediately preceding 10 data points are equally weighted.
And now this makes a lot of sense.
However it might be the case that that we want to give more weight or a higher weight to more recent
observations and that we can do this with an exponential weighting.
So let's first of all calculate the simple moving average of the close column here with a rolling window
of 10
and actually the appropriate method to calculate exponentially away that moving averages is the E W
M method and from a mathematical perspective there are plenty of options how to exponentially weight
the observations and the parameters to do so span and half life.
So let's have a look here.
So you have the parameter span and half life and also alpha but actually I do not want to go here and
to a mathematical details and therefore a quite popular way is to use a span.
So here we have also the span parameter and here in the IWM method we actually pass the 10 to the span
parameter and also the FAA the min periods parameter and we pass also the value of 10.
So we need at least 10 observations to calculate an exponentially weighted moving average.
And finally we change the mean method so let's have a look.
So these are here the exponentially awaited moving averages.
And now we can also create two new columns a simple moving average and exponentially weighted moving
average and we actually append these new columns uh to our data frame S&P 500.
So let's do this here and let's have a look.
So here we have our closing prices.
And then the simple moving averages and the exponentially weighted moving averages.
So yeah at the very end we can see we have uh nominal values here.
And now the question is if and to what extent.
Simple moving average is an exponentially weighted moving average just behave in a different way.
And uh the best way to discover this is actually with a graph.
So we're actually plotting here the last two columns.
So we are selecting the last two columns.
And use the plot method.
So let's have a look here.
And we can see here in blue will the line for simple moving averages.
And in green for exponentially moving averages and since exponentially weighted average just place more
weight on more recent observations.
So the green graph actually adapts faster to change as compared to the equal weighted versions of here.
The blue graph.
So for example we can see here that the S&P 500 declines and the it seems like that the green curve
adapts to the declining prices faster than the blue curve.
And also here we can see that exponentially weighted average US actually we are moving faster are adapting
faster to most the recent trends.
So this is the rationale behind exponentially weighted moving averages.
And I hope to see you also in the next video by.